using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace MiniAnimation
{

    public class TestMiniSeq : MonoBehaviour
    {
        [SerializeField] SeqItem[] items;
        private async void Start()
        {
            DebugAction.AddAction("执行序列1", async () =>
            {
               items[0].DoAnimAsync();
            });
            DebugAction.AddAction("执行序列2", async () =>
            {
                items[1].DoAnimAsync();
            });
            DebugAction.AddAction("执行序列toal", async () =>
            {
                foreach (var item in items)
                {
                    item.DoAnimAsync();
                }
            });
            DebugAction.AddAction("顺序执行 1、2序列",async ()=>{
                foreach (var item in items)
                {
                    await item.DoAnimAsync();
                }
            });
            DebugAction.AddAction("直接用动画回调",async ()=>{
                 StartCoroutine(Animation(()=>{
                    StartCoroutine(Animation(()=>{
                        Debug.Log("动画播放完成");
                    }));
                 }));
            });
            DebugAction.AddAction("使用Task做扁平化",async ()=>{
                await TaskAnimation();
                await TaskAnimation();
                Debug.Log("动画播放完成");
            });
        }
        //动画扁平化
        async Task TaskAnimation()
        {
            TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
            StartCoroutine(Animation(() =>
            {
                tcs.SetResult(null);
            }));
            await tcs.Task;
        }

        /// <summary>
        /// 模拟动画
        /// </summary>
        /// <returns></returns>
        IEnumerator Animation(Action callback)
        {
            //等待3秒模拟动画
            yield return new WaitForSeconds(1);
            callback?.Invoke();
        }

    }
    
}